﻿using ConsoleApp_EXL;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET
{
    public static class GeneratePlcTags
    {
        /// <summary>
        /// 生成PLC变量表
        /// </summary>
        /// <param name="rows">实体类列表</param>
        /// <param name="path"></param>
        public static void GeneratePlcTagsFromTable(List<InputTableRow> rows, string ?path)
        {
            using (FileStream exlFile = new FileStream(path + @"/PLCTags.xlsx", FileMode.Create, FileAccess.ReadWrite))
            {
                IWorkbook workBook = new XSSFWorkbook();

                ISheet sheet20 = workBook.CreateSheet("PLC Tags");

                IRow row = sheet20.CreateRow(0);

                //写入表头
                row.CreateCell(0).SetCellValue("Name");
                row.CreateCell(1).SetCellValue("Path");
                row.CreateCell(2).SetCellValue("Data Type");
                row.CreateCell(3).SetCellValue("Logical Address");
                row.CreateCell(4).SetCellValue("Comment");
                row.CreateCell(5).SetCellValue("Hmi Visible");
                row.CreateCell(6).SetCellValue("Hmi Accessible");
                row.CreateCell(7).SetCellValue("Hmi Writeable");
                row.CreateCell(8).SetCellValue("Typeobject ID");
                row.CreateCell(9).SetCellValue("Version ID");
                row.CreateCell(10).SetCellValue("BelongsToUnit");




                //写入内容
                for (int i = 0; i < rows.Count; i++)
                {
                    IRow row_2 = sheet20.CreateRow(i + 1);
                    row_2.CreateCell(0).SetCellValue(rows[i].Adress);
                    row_2.CreateCell(1).SetCellValue("IO");
                    if (rows[i].Type == "Bool") { row_2.CreateCell(2).SetCellValue("Bool"); }
                    if (rows[i].Type == "Word") { row_2.CreateCell(2).SetCellValue("Word"); }
                    row_2.CreateCell(3).SetCellValue("%" + rows[i].Adress);
                    row_2.CreateCell(4).SetCellValue(rows[i].Notes);
                    row_2.CreateCell(5).SetCellValue("True");
                    row_2.CreateCell(6).SetCellValue("True");
                    row_2.CreateCell(7).SetCellValue("True");
                }

                workBook.Write(exlFile, false); //写入目标表格

            } ;  

            
        }
    }
}
